package src;

import java.io.BufferedWriter;
import java.util.Map;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class dataManager
{

    private Map<Integer, String> puffer;
    private SynchronizedCounter logSeqNr;
    private ArrayList<Integer> openTransactions;

    public dataManager()
    {
        puffer = new ConcurrentHashMap<Integer, String>();
        logSeqNr = new SynchronizedCounter();
        openTransactions = new ArrayList<Integer>();
    }

    /*
     * Schreibt in den Puffer
     */
    public void write(int taid, int pageid, String data)
    {
        Integer LogNr = writeLogFile(taid, pageid, data);
        puffer.put(pageid, String.valueOf(taid + ";" + LogNr + ";" + data));
    }

    /*
     * Schranke mit den fünf Einträgen
     */
    public void pruefePuffer(int taid)
    {
        if (puffer.size() > 5)
        {
            System.out.println(puffer);
            Iterator<Integer> keySetItr = puffer.keySet().iterator();
            while (keySetItr.hasNext())
            {
                Integer key = keySetItr.next();
                String gesuchteTaid = puffer.get(key).substring(0, puffer.get(key).indexOf(";"));
                if (Integer.parseInt(gesuchteTaid) == taid)
                {
                    writeToFile(taid, key, puffer.get(key));
                    writeCommitLog(taid, key, puffer.get(key));
                    try
                    {
                        keySetItr.remove();
                        System.out.println("--> Lösche Seite " + key + " aus Transaktion " + taid);
                    }
                    catch (Exception e)
                    {
                        // TODO: handle exception
                    }
                }
            }
        }
    }

    /*
     * Schreibt in Datei
     */
    public void writeToFile(int taid, int pageid, String data)
    {
        try
        {
            File file = new File("./Data/Seite" + String.valueOf(pageid) + ".txt");
            // System.out.println(file.getAbsolutePath());
            FileWriter fw = new FileWriter(file);

            fw.write(taid + ";" + pageid + ";" + data);
            System.out.println("Schreibe Seite " + pageid + " aus Transaktion " + taid + " in Datei");
            fw.flush();
            fw.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /*
     * Lesen
     */
    public void readLogFile()
    {
        try
        {
            FileReader fr = new FileReader("./Data/Logfile.txt");
            BufferedReader br = new BufferedReader(fr);
            String s;
            while (true)
            {
                String line = br.readLine();
                if (line != null)
                {
                    if (line.indexOf("Commit") > -1)
                    {
                        System.out.println(line);
                    }
                }
                else
                {
                    break;
                }
            }
            fr.close();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * Schreiben der Log-Datei
     */
    public int writeLogFile(int taid, int pageid, String data)
    {
        int TempLogSeqNr;
        TempLogSeqNr = logSeqNr.incrementAndGet();
        try
        {
            FileWriter fw = new FileWriter("./Data/Logfile.txt", true);
            BufferedWriter out = new BufferedWriter(fw);
            out.write(TempLogSeqNr + ";" + taid + ";" + pageid + ";" + data);
            out.newLine();
            out.close();
            fw.close();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return TempLogSeqNr;
    }

    /*
     * Log der Commit-Übertragung
     */
    public int writeCommitLog(int taid, int pageid, String data)
    {
        int TempLogSeqNr;
        TempLogSeqNr = logSeqNr.incrementAndGet();
        try
        {
            FileWriter fw = new FileWriter("./Data/Logfile.txt", true);
            BufferedWriter out = new BufferedWriter(fw);
            out.write("Commit " + TempLogSeqNr + ";" + taid + ";" + pageid + ";" + data);
            out.newLine();
            out.close();
            fw.close();

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return TempLogSeqNr;
    }
}
